<template>
  <!-- 购物车不空（长度不为0） -->
  <view class="cart-container" v-if="cart.length !== 0">
    <!-- 收货地址组件 -->
    <my-address></my-address>
    
    <!-- 商品列表标题区域 -->
    <view class="cart-title">
      <!-- 图标 -->
      <uni-icons type="shop" size="18"></uni-icons>
      <!-- 文本 -->
      <text class="cart-title-text">购物车</text>
    </view>
    
    <!-- uni-swipe-action用于实现右滑出现删除 -->
    <uni-swipe-action>
      <!-- 商品信息区域 -->
      <block v-for="(goods,i) in cart" :key="i">
        <!-- options参数是储存删除的内容和样式 -->
        <uni-swipe-action-item :right-options="options" @click="swiperItemClickHandler(goods)">
          <!-- 当触发自定义的radio-change函数时（通过在子组件中$emit触发），触发radioChangeHandler函数 -->
          <my-goods :goods="goods" :showRadio="true" @radio-change="radioChangeHandler" :showNum="true" @num-change="numberChangeHandler">
          </my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    
    <!-- 结算组件 -->
    <my-settle></my-settle>
    
  </view>
  
  <!-- 空白购物车区域 -->
  <view class="empty-cart" v-else>
    <image src="/static/shop.png" class="empty-image"></image>
    <text class="tip-text">空空如也</text>
    <view class="goToCate" @click="goToCate">
      <text class="goToCate-text">去逛逛</text>
      </view>
  </view>
  
</template>

<script>
  // 导入mixins文件
  import tarbbarMix from '@/mixins/tabbar-badge.js'
  import {mapState,mapMutations} from 'vuex'
  
  
  export default {
    mixins:[tarbbarMix],
    
    computed:{
      ...mapState('m_cart',['cart'])
    },
    
    data() {
      return {
        // 右滑删除的内容
        options:[{
          text:'删除',
          style:{
            backgroundColor:'#C00000'
          }
        }]
      };
    },
    
    methods:{
      ...mapMutations('m_cart',['updateGoodsState','updateGoodsCount','removeGoodsById']),
      
      // 勾选状态变化
      radioChangeHandler(e){
        // console.log(e)
        this.updateGoodsState(e)
      },
      
      // 数量变化
      numberChangeHandler(e){
        // console.log(e)
        // 如果数量没到0
        if(e.goods_count !== 0)
        {this.updateGoodsCount(e)}
        
        // 如果数量到0
        else{
          this.swiperItemClickHandler(e.goods_id)
        }
        
      },
      
      // 删除商品
      swiperItemClickHandler(goods){
        // console.log(goods_id)
        this.removeGoodsById(goods)
      },
      
      goToCate(){
        uni.switchTab({
          url:'/pages/cate/cate'
        })
      }
    }
  }
</script>

<style lang="scss">
  .cart-container{
    // 距底边50px,用于防止结算组件防止覆盖购物车内容
    padding-bottom: 50px;
  }
  
.cart-title{
  height:40px;
  display: flex;
  align-items: center;
  padding-left: 5px;
  border-bottom: 1px solid #efefef;
  
  .cart-title-text{
    font-size: 14px;
    margin-left: 10px;
  }

}

.empty-cart{
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;
    
  .empty-image{
      width:90px;
      height: 90px;
    }
  .tip-text{
      font-size:12px;
      color:gray;
      margin-top: 15px;
    }
  .goToCate{
    width: 100px;
    height: 30px;
    margin-top:15px;
    background-color: #66ccff;
    border-radius: 15px;
    color:white;
    .goToCate-text{
      margin-left:25px;
      margin-top:20px;
    }
  }
  }
</style>
